package com.example.adapter;

import java.util.ArrayList;

import com.example.android.HomeTest;
import com.example.androidtest.ImageThread;
import com.example.androidtest.R;
import com.example.fragment.Person_Fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class MyHomeAdapter extends PagerAdapter{
	ArrayList<HomeTest> list = new ArrayList<>();
	Person_Fragment person;
	private String result;
	private Handler handler;
	private String url = "http://www.mengxianyi.net/one/img/"+"homepage"+".jpg";
	private TextView textView_title;
	private TextView textView_auther;
	private TextView textView_content;
	private ImageView imageView_image;
	private Context context;//上下文 承接
	private Bitmap imageResult;
	private HomeTest bean;
	
	
	
	
	public MyHomeAdapter(Context context,ArrayList<HomeTest> list) {//解析后的数据传递过来
		this.list = list;
		this.context = context;
	}

	@Override

	public int getCount() {
		// TODO Auto-generated method stub
		return list != null&&list.size()>0? list.size():0;//判断传来的解析后获取的数据 有没有内容 可以判断有没有传过来解析的数据
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0 == arg1;
	}

	@Override
	public View instantiateItem(ViewGroup container, int position) {
		// TODO Auto-generated method stub
		View view = LayoutInflater.from(context).inflate(R.layout.home, null);
		
		
		
		handler = new Handler(new Handler.Callback() {
			
			@Override
			public boolean handleMessage(Message msg) {
				if(msg.what ==200){
					imageResult = (Bitmap) msg.obj;
					
				}
				return false;
			}
		});
		bean = list.get(position);
		new ImageThread(handler, "http://www.mengxianyi.net/one/img/"+bean.getImage()+".jpg").start();
		textView_title =(TextView)view.findViewById(R.id.tv_home_title);
		textView_auther =(TextView)view.findViewById(R.id.tv_home_author);
		textView_content =(TextView)view.findViewById(R.id.tv_home_content);
		imageView_image =(ImageView)view.findViewById(R.id.im_home_image);
		textView_title.setText(bean.getTitle());
		textView_auther.setText(bean.getQuthor());
		textView_content.setText(bean.getContent());
		imageView_image.setImageBitmap(imageResult);
		container.addView(view);

		return view;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// TODO Auto-generated method stub
	}
	
}
